<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Home\Behaviors;

use Think\Behavior;

/**
 * 用户注册成功后的行为扩展
 */
class UserBindBehavior extends Behavior{
	
	public function run(&$params) {
		$userId = $params ['id'];
		
		$setting = getGlobleConfig();
		
		if ($setting ['register_coupon'] != '0') {
			$CouponModel = D('CouponCode');
			$id = $setting ['register_coupon'];
			$registerCoupon = $CouponModel->getCouponInfo($id);
			
			if ($registerCoupon) {
				$UserCouponModel = D('UserCoupon');
				$time = time();
				$expires_time = $registerCoupon ['expiry_type'] == '3'? time() + $registerCoupon ['expiry'] * 24 * 3600:$registerCoupon ['expiry'];
				$couponData = array(
					'user_id' => $userId,
					'coupon_title' => $registerCoupon ['title'],
					'coupon_id'	=> $registerCoupon ['c_id'],
					'c_type'	=> $registerCoupon ['c_type'],
					'money_max'	=> $registerCoupon ['money_max'],
					'money_reduce'	=> $registerCoupon ['money_reduce'],
					'money_amount'	=> $registerCoupon ['money_amount'],
					'expiry_type'	=> $registerCoupon ['expiry_type'],
					'expires_time'	=> $expires_time,
					'created' => $time,
				);
					
				$hasReceiveCoupon = $UserCouponModel->where(array('coupon_id' => $registerCoupon ['id'],
						'user_id' => $userId
				))->find();
					
				if (!$hasReceiveCoupon) {
					$UserCouponModel->add($couponData);
				}
			}
		}
	}
}

?>